﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8" />
    <title>Ping: UdpEchoClient.cpp Source File</title>
    <link href="/Content/Help.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/tabs.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="header">
        <img id="logo" alt="StateForge" src="/Content/Images/StateForgeLogo.png" />
        <div id="title">
            <h1>
                Examples using state machines generated by StateBuilderCpp
            </h1>
            <h3>
                Ping: UdpEchoClient.cpp Source File
            </h3>
        </div>
        <div id="menucontainer">
            <ul id="menu">
                <li><a href="/">Home</a></li>
                <li><a href="/Home/Download">Downloads</a></li>
                <li><a href="/Help/state-machine-tools.aspx">Help</a></li>
                <li><a href="/Home/Faq">F.A.Q</a></li>
                <li><a href="/Home/About">Contact us</a></li>
            </ul>
        </div>
    </div>
<!-- Generated by Doxygen 1.7.4 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">UdpEchoClient.cpp</div>  </div>
</div>
<div class="contents">
<a href="_udp_echo_client_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00002"></a>00002 <span class="preprocessor">#include &lt;QtNetwork/QUdpSocket&gt;</span>
<a name="l00003"></a>00003 <span class="preprocessor">#include &lt;QsLog.h&gt;</span>
<a name="l00004"></a>00004 <span class="preprocessor">#include &lt;QsLogDest.h&gt;</span>
<a name="l00005"></a>00005 
<a name="l00006"></a>00006 <span class="preprocessor">#include &lt;<a class="code" href="_udp_echo_client_8h.html">UdpEchoClient.h</a>&gt;</span>
<a name="l00007"></a>00007 
<a name="l00008"></a><a class="code" href="class_udp_echo_client.html#a451d983bc262ba9749a711f65de8c77d">00008</a> <a class="code" href="class_udp_echo_client.html#a451d983bc262ba9749a711f65de8c77d">UdpEchoClient::UdpEchoClient</a>(QObject *pParent) : QObject(pParent),
<a name="l00009"></a>00009   m_address(QString(<span class="stringliteral">&quot;127.0.0.1&quot;</span>)),
<a name="l00010"></a>00010   m_port(2012)
<a name="l00011"></a>00011 {
<a name="l00012"></a>00012     QLOG_DEBUG();
<a name="l00013"></a>00013     <a class="code" href="class_udp_echo_client.html#ade73b42026a761a494025b7c753c9ac4">m_pUdpSocket</a> = <span class="keyword">new</span> QUdpSocket(<span class="keyword">this</span>);
<a name="l00014"></a>00014 
<a name="l00015"></a>00015     <span class="keywordflow">if</span>(<a class="code" href="class_udp_echo_client.html#ade73b42026a761a494025b7c753c9ac4">m_pUdpSocket</a>-&gt;bind(<a class="code" href="class_udp_echo_client.html#a88d2805cad5ad2dba77336583e5c6702">m_port</a>, QUdpSocket::ShareAddress) == <span class="keyword">false</span>){
<a name="l00016"></a>00016         QLOG_ERROR() &lt;&lt; <span class="stringliteral">&quot;Cannot listen on port &quot;</span> &lt;&lt; <a class="code" href="class_udp_echo_client.html#a88d2805cad5ad2dba77336583e5c6702">m_port</a> &lt;&lt; <span class="stringliteral">&quot;, error &quot;</span> &lt;&lt; <a class="code" href="class_udp_echo_client.html#ade73b42026a761a494025b7c753c9ac4">m_pUdpSocket</a>-&gt;errorString();
<a name="l00017"></a>00017     }
<a name="l00018"></a>00018     
<a name="l00019"></a>00019     connect(<a class="code" href="class_udp_echo_client.html#ade73b42026a761a494025b7c753c9ac4">m_pUdpSocket</a>, SIGNAL(readyRead()), <span class="keyword">this</span>, SLOT(<a class="code" href="class_udp_echo_client.html#ab828ca963e7387d8f28cbe8c5f88d8e6">onReply</a>()));
<a name="l00020"></a>00020     <a class="code" href="class_udp_echo_client.html#a6b5875ed18c71a2a7966fea4362aa814">m_datagram</a>.append(<span class="stringliteral">&quot;Ciao Biloute&quot;</span>);
<a name="l00021"></a>00021 }
<a name="l00022"></a>00022 
<a name="l00023"></a><a class="code" href="class_udp_echo_client.html#a1f32d159aaac158aed84b99a11bd7512">00023</a> <a class="code" href="class_udp_echo_client.html#a1f32d159aaac158aed84b99a11bd7512">UdpEchoClient::~UdpEchoClient</a>()
<a name="l00024"></a>00024 {
<a name="l00025"></a>00025     QLOG_DEBUG();
<a name="l00026"></a>00026 }
<a name="l00027"></a>00027 
<a name="l00028"></a><a class="code" href="class_udp_echo_client.html#ab828ca963e7387d8f28cbe8c5f88d8e6">00028</a> <span class="keywordtype">void</span> <a class="code" href="class_udp_echo_client.html#ab828ca963e7387d8f28cbe8c5f88d8e6">UdpEchoClient::onReply</a>()
<a name="l00029"></a>00029 {
<a name="l00030"></a>00030     QLOG_INFO();
<a name="l00031"></a>00031     QByteArray datagram;
<a name="l00032"></a>00032     QHostAddress address;
<a name="l00033"></a>00033     quint16 port;
<a name="l00034"></a>00034     
<a name="l00035"></a>00035     datagram.resize(<a class="code" href="class_udp_echo_client.html#ade73b42026a761a494025b7c753c9ac4">m_pUdpSocket</a>-&gt;pendingDatagramSize());
<a name="l00036"></a>00036     <a class="code" href="class_udp_echo_client.html#ade73b42026a761a494025b7c753c9ac4">m_pUdpSocket</a>-&gt;readDatagram(datagram.data(),datagram.size(), &amp;address, &amp;port);
<a name="l00037"></a>00037     emit <a class="code" href="class_udp_echo_client.html#a7e5e7c8b80ab2802c056b04370e2f44c">reply</a>();
<a name="l00038"></a>00038 }
<a name="l00039"></a>00039 
<a name="l00040"></a><a class="code" href="class_udp_echo_client.html#af9885baea55efc4fc611605e087d6427">00040</a> <span class="keywordtype">void</span> <a class="code" href="class_udp_echo_client.html#af9885baea55efc4fc611605e087d6427">UdpEchoClient::send</a>()
<a name="l00041"></a>00041 {
<a name="l00042"></a>00042     QLOG_INFO();
<a name="l00043"></a>00043     <a class="code" href="class_udp_echo_client.html#ade73b42026a761a494025b7c753c9ac4">m_pUdpSocket</a>-&gt;writeDatagram(<a class="code" href="class_udp_echo_client.html#a6b5875ed18c71a2a7966fea4362aa814">m_datagram</a>.data(), <a class="code" href="class_udp_echo_client.html#a6b5875ed18c71a2a7966fea4362aa814">m_datagram</a>.size(), <a class="code" href="class_udp_echo_client.html#ab7e8f9607c4478554bebde8c4adc2147">m_address</a>, <a class="code" href="class_udp_echo_client.html#a88d2805cad5ad2dba77336583e5c6702">m_port</a>);
<a name="l00044"></a>00044 }
<a name="l00045"></a>00045 
<a name="l00046"></a>00046 <span class="preprocessor">#include &quot;UdpEchoClient.moc&quot;</span>
</pre></div></div>
</div>
﻿<div id="footer">
    <hr />
    <p>
        Copyright &copy; 2011 StateForge.</p>
    <hr />
</div>
</body> </html> 
